<?php

namespace app\api\controller;

use app\common\logic\CartLogic;
use app\common\logic\MessageLogic;
use app\common\logic\UsersLogic;
use app\common\logic\OrderLogic;
use app\common\logic\CouponLogic;
use think\Page;
use think\Request;
use think\Verify;
use think\Db;
use think\Session;
class Video extends ApiCommon
{
	
	/*
    * 初始化操作
    */
    public function _initialize()
    {
//      parent::_initialize();
//      if (session('?user')) {
//          $user = session('user');
//          $user = M('users')->where("user_id", $user['user_id'])->find();
//          session('user', $user);  //覆盖session 中的 user
//          $this->user = $user;
//          $this->user_id = $user['user_id'];
////          $this->assign('user', $user); //存储用户信息
//      }
    }
    
	//获得videoList
	public function getVideoList(){
		
		$page = I('page/d',0);
		$stype = I('showtype',0);
		$uid = I('uid/d',0);
		
		$arr = getVideoList($page,$stype,$uid);
		
		$this->returnMsg('ok','',$arr);
	}
	
	//点赞
	public function AddlaudNum(){
		$uid = I('uid/d',0);
		$vid = I('vid/d',0);
		
		$RlaudNum = Db::query("select laudNum from tp_video where vid=$vid");
		if(count($RlaudNum)<=0){
			$this->returnMsg('-1','视频不存在');
		}
		
		$laudNum = $RlaudNum[0]['laudNum'];
		
		$result = Db::query("select * from tp_video_laud where vid = $vid and uid= $uid");
		if(count($result)>0){
			$laudid = $result[0]['id'];
			$laudNum -= 1;
			$laudNum = $laudNum<0?0:$laudNum;
			
			Db::query("update tp_video set laudNum=$laudNum where vid=$vid");
			Db::query("delete from tp_video_laud where id = $laudid");
			$this->returnMsg('ok','取消点赞成功',['laudNum'=>$laudNum,'vid'=>$vid,'isadd'=>0]);
		}
		else{
			$laudNum += 1;
			Db::query("update tp_video set laudNum=$laudNum where vid=$vid");
			$curtime = time();
			Db::query("insert into tp_video_laud(vid,uid,atime)values($vid,$uid,$curtime)");
			$this->returnMsg('ok','成功',['laudNum'=>$laudNum,'vid'=>$vid,'isadd'=>1]);
		} 
	}
	
	//评论列表
	public function video_comm_list(){
		$vid = I('vid/d',0);
		$page = I('page/d',0);
		$pagesize = C('PAGESIZE');
		$startPage = $pagesize * $page;
//		$videolist = Db::query("select tp_video.*, tp_goods.goods_name from tp_video,tp_goods where goodsid = goods_id and vid = $vid");	
		$vcommlist = Db::query("select tp_video_comment.*,tp_users.nickname,head_pic from tp_video_comment,tp_users 
		where tp_video_comment.vid = $vid and tp_video_comment.uid = tp_users.user_id and isshow = 1 and parent_id = 0 order by addtime desc LIMIT ?,?",
		[$startPage,$pagesize]);
		
		$replyList = M('video_comment')->where(['isshow' => 1,'vid' => $vid, 'parent_id' => ['>', 0]])
		->order("addtime desc")->select();
		
//		foreach($vcommlist as $k=>$v){
//			$vcommlist[$k]['replyList'] = M('video_comment')->where(['isshow' => 1, 'vid' => $vid, 'parent_id' => $v['id']])->order("addtime desc")->select();
//		}
		
		$this->returnMsg('ok','',['arr'=>$vcommlist,'rarr'=>$replyList]);	
	}
	
	//添加评论
	public function addcomment(){
		$data = I('post.');
		$parent_id = I('parent_id/d',0);
		
		$data['addtime'] = time();
		$data['isshow'] = 1;
        
        if($parent_id!=0){
			$user = Session::get('user');
			$uname = $user['nickname'];
			$data['username'] = $uname;
		}
		
        M('video_comment')->add($data);
        
        $vid = I('vid/d',0);
        M('video')->where(['vid'=>$vid])->setInc('commentnum');       
        
        $this->returnMsg('ok','评论成功');
	}
	
	public function delcomment(){
		$uid = I('uid/d',0);
		$id = I('id/d',0);
		if($id == 0){
			$this->returnMsg('-1','删除失败');
		}
		
		$result = M('video_comment')->where(['id'=>$id])->select();
		if(count($result)>0 && $result[0]['uid'] == $uid){
			M('video_comment')->where(['id'=>$id])->delete();
			M('video_comment')->where(['parent_id'=>$id])->delete();
			$this->returnMsg('ok','删除成功');
		}
		
		$this->returnMsg('-1','不是你的评论不可删除哦');
	}
	
	//获得单个视频信息
	public function getVideoById(){
		$vid = input('vid/d',0);
		$uid = input('uid/d',0);
		
		$videolist = M('video')->where(['vid'=>$vid])->select();

		$vids = [];
		foreach($videolist as $key => $value){
			array_push($vids, $value['vid']);		
			$vgoods = M('goods')->whereIn('goods_id',$value['goods_id'])->field('goods_id,goods_name,original_img,shop_price')->select();
			$videolist[$key]['goods'] = $vgoods;
		}
		
		$backArr = [];
		$backArr['varr'] = $videolist;
		$backArr['vluadarr'] = [];
		
		if(count($vids)>0){
			$vidsStr = implode(',', $vids);
			$v_laud = M('video_laud')->where(['uid'=>$uid])->whereIn('vid',$vidsStr)->select();
			$backArr['vluadarr'] = $v_laud;
		}
		$this->returnMsg('ok','',$backArr);
	}
	
}
?>